/**
 * 
 */
package edu.netjava.LinkList;

/**
 * @author tan this is a class defining list
 * 
 */
public class LinkList {

	LinkList linklist;
	Node headNode = null;
	Node currentNode = headNode;
	int index = -1;

	/**
	 * the constructor to initial
	 */
	public LinkList() {

	}

	/**
	 * do the add action
	 */
	public void add(String object) {

		Node node = new Node();
		node.setData(object);
		node.setNext(null);

		index++;
		/**
		 * set the head node
		 */
		if (headNode == null) {
			headNode = node;
			System.out.println("headNode:" + node.getData());
			currentNode = headNode;
		} else {
			currentNode.setNext(node);// add node
			currentNode = node;
		}

	}

	/**
	 * 
	 * @param index :
	 *            the location of the inserted element
	 * @param str :
	 *            the value of the element
	 */
	public void insertNode(int index, String str) {

		Node node = headNode;
		Node nodeDai = new Node();
		Node newNode = new Node();
		newNode.setData(str);
		if (index < 0 || index > this.index) {
			System.out.println("========================");
			System.out.println("Warning !!!!\n" + "index out of bounds!");
			System.out.println("========================");
			return;
		}

		if (index == 0) {
			newNode.setNext(node);
			headNode = newNode;
		}

		for (int i = 0; i < index; i++) {

			if (i == index - 1) {
				nodeDai = node.getNext();
				node.setNext(newNode);
				newNode.setNext(nodeDai);
				break;

			}

			node = node.getNext();
		}

		index++;
	}

	/**
	 * 
	 * @param index :
	 *            the location of the deleted element
	 * @return : the deleted element
	 */
	public Object delete(int index) {
		// deal when the index = 0
		if (index == 0) {
			headNode = headNode.getNext();
		}

		// when index is not zero
		Node delNode = headNode;

		for (int i = 0; i < index; i++) {

			if (i == index - 1) {
				delNode.setNext(delNode.getNext().getNext());
				index--;
				return delNode.getNext().getData();
			}

		}
		delNode = delNode.getNext();
		return null;
	}

	/**
	 * 
	 * @param index:location
	 *            of the finding node
	 * @return :the found node
	 */
	public Node findNode(int index) {

		if (index < 0 || index > this.index) {
			System.out.println("========================");
			System.out.println("Warning !!!!\n" + "index out of bounds!");
			System.out.println("========================");
			return null;
		}

		Node node = headNode;
		for (int i = 0; i < index; i++) {
			node = node.getNext();
		}
		return node;
	}

	/**
	 * 
	 * @param node :
	 *            a node to return its next
	 * @return : result
	 */
	public boolean hasNext(Node node) {
		return (node.getNext() != null);
	}

	/**
	 * @return
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		return linklist.toString();
	}

	/**
	 * print all elements in the list
	 */
	public void printAll() {
		Node node = new Node();
		node = headNode;
		int i = 0;
		while (node != null) {
			output("the " + i + " rh element:" + node.getData());
			node = node.getNext();
			i++;
		}

	}

	/**
	 * 
	 * @param object
	 *            :the data to print
	 */
	public static void output(Object object) {
		System.out.println(object);
	}

}
